package com.example.MyExam.controller;

import com.example.MyExam.model.Student;
import com.example.MyExam.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class UserController {


    @Autowired
    UserService userService;

    /**
     *获取所有用户信息
     */
    @CrossOrigin(origins = "http://localhost:8080")
    @GetMapping("/users")
    public List<Student> getAllUsers(){
        return userService.list();
    }

    /**
     * 根据ID查询用户信息
     * URL_ADDRESS: http://localhost:8089/user/{id}
     * @return
     */
    @CrossOrigin(origins = "http://localhost:8080")
    @GetMapping("/user/{id}")
    public Student getUserById(@PathVariable("id") Integer id){
        return userService.getById(id);
    }

    @PostMapping("/user")
    public String addUser(@RequestBody Student user){
        if(userService.save(user)){
            return "添加成功";
        }else {
            return "添加失败";
        }
    }
    @PutMapping("/user")
    public String updateUser(@RequestBody Student user){
        if(userService.updateById(user)){
            return "修改成功";
        }else {
            return "修改失败";
        }
    }
    @DeleteMapping ("/user/{id}")
    public String deleteUser(@PathVariable("id") Integer id){
        if(userService.removeById(id)){
            return "删除成功";
        }else {
            return "删除失败";
        }
    }
}












